package edu.xidian.onlinedocument.controller.configuration;

import edu.xidian.onlinedocument.handler.interceptor.WebSocketInterceptorTest;
import edu.xidian.onlinedocument.websocket.EditorWebSocketHandler;
import edu.xidian.onlinedocument.handler.interceptor.EditorWebSocketInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class EditorWebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        /*registry.addHandler(editorWebSocketHandler(), "/websocket/documents")
                .addInterceptors(new EditorWebSocketInterceptor(docId, userService))
                .setAllowedOrigins("*");
        */
        registry.addHandler(editorWebSocketHandler(), "/websocket/documents/{docId}")
                .addInterceptors(new EditorWebSocketInterceptor())
                .setAllowedOriginPatterns("*")
                // .withSockJS()
                ;

        registry.addHandler(editorWebSocketHandler(), "/websocket/test/documents/{docId}/{userId}")
                .addInterceptors(new WebSocketInterceptorTest())
                .setAllowedOriginPatterns("*")
                // .withSockJS()
                ;
    }

    public WebSocketHandler editorWebSocketHandler() {
        return new EditorWebSocketHandler();
    }
}
